#!/usr/bin/python
# coding=utf-8

import os


def get_addonname_from_servicepath(service_path):
    absbasedir = os.path.dirname(os.path.abspath(service_path))
    return os.path.split(absbasedir)[1]


def get_addon_install_dir(config, addonname):
    """
    config: AddOnConfig.inst()
    """
    install_dir = config.get_install_dir()
    return os.path.join(install_dir, addonname)


def get_addon_ini_file_path(config, addon_name):
    inst_dir = get_addon_install_dir(config, addon_name)
    inifile_suffix = config.get_iniconf_suffix()
    return os.path.join(inst_dir, addon_name, addon_name + inifile_suffix)


def chdirmode(path, mode):
    """
    @description: 修改指定路径权限
    @param path: 路径
    @param mode: 0644.八进制数表示权限, 同chmod
    """
    os.chmod(path, mode)
    if os.path.isdir(path):
        dirs = os.listdir(path)
        for d in dirs:
            chdirmode(os.path.join(path, d), mode)
